package br.com.teste;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.scp.modelo.Participante;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static ArrayList<String> lista = new ArrayList<>();
       
    public Servlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	exercutar(request, response);
    }
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		exercutar(request, response);
	}
	private void exercutar(HttpServletRequest request, HttpServletResponse response) throws IOException {

		if(request.getParameter("cadastroParticipanteAndroid") != null){
		
			System.out.println("TO AQUI NO CADASTRO DE PARTICIPANTE");
			
			String participanteJson = request.getParameter("cadastroParticipanteAndroid");
			
			response.setContentType("application/json");
			
			Gson g = new Gson();
			Participante p = g.fromJson(participanteJson, Participante.class);
			
			String nome2 = p.getNome();
			String email = p.getEmail();
			String cpf = p.getCpf();
			
			System.out.println("nome = "+nome2);
			System.out.println("emial = "+email);
			System.out.println("cpf = "+cpf);
			
			PrintWriter out = response.getWriter();
			
		}
		if(request.getParameter("listar") != null){
				
			System.out.println("to na lista");
			
			response.setContentType("application/json");    
			response.setCharacterEncoding("UTF-8");
			
			Participante p = new Participante();
			
			p.setCelular("celular teste");
			p.setCpf("cfp teste");
			p.setEmail("email teste");
			p.setMatricula("matricula teste");
			p.setNome("nome teste");
			
			PrintWriter pw = response.getWriter();
			
				JsonObject gSON = new JsonObject();
				gSON.addProperty("celular", p.getCelular());
				gSON.addProperty("cpf", p.getCpf());
				gSON.addProperty("email", p.getEmail());
				gSON.addProperty("matricula", p.getMatricula());
				gSON.addProperty("nome", p.getNome());
			
			pw.print(gSON);
		}
		
		if (request.getParameter("nome") != null){
			
			String nome = request.getParameter("nome");
			System.out.println("nome  = "+ nome);
			
			lista.add(nome);
			
			PrintWriter out = response.getWriter();
			
			out.println("nome cadastrado  = "+ nome);
		
		}
	}
}
